//
//  ReadViewController.m
//  Relax
//
//  Created by yanghuiyan on 16/4/18.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "ReadViewController.h"
#import "DCPicScrollView.h"
#import "ReadQuestionViewController.h"
#import "ReadEssayViewController.h"
#import "ReadSerialViewController.h"
@interface ReadViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain) YHYBaseView *topView;

@property (nonatomic, retain) YHYBaseCollectionView *readCollectionView;

@property (nonatomic, retain) NSMutableArray *dataSource;

@property (nonatomic, retain) NSMutableArray *imageArray;

@property (nonatomic, retain) NSMutableArray *essayArr;

@property (nonatomic, retain) NSMutableArray *serialArr;

@property (nonatomic, retain) NSMutableArray *questionArr;

@property (nonatomic, retain) EssayModel *essayModel;

@property (nonatomic, retain) SerialModel *serialModel;

@property (nonatomic, retain) QuestionModel *questionModel;

@property (nonatomic, retain) ReadBannerModel *bannerModel;//轮播

@property (nonatomic, retain) DCPicScrollView *picScrollView;

@property (nonatomic, assign) NSInteger item;

@end

@implementation ReadViewController

- (void)dealloc {
    
    [_readCollectionView release];
    [_imageArray release];
    [_serialArr release];
    [_questionArr release];
    [_essayModel release];
    [_serialModel release];
    [_bannerModel release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.dataSource = [NSMutableArray array];
    self.imageArray = [NSMutableArray array];
    self.essayArr = [NSMutableArray array];
    self.serialArr = [NSMutableArray array];
    self.questionArr = [NSMutableArray array];
    
    [self creatView];
    [self dataHandle];
    
//    self.item = 0;
    // 接收通知跳转下一页
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:@"jumpNext" object:nil];
   

}

- (void)notificationAction:(NSNotification *)notification {
        
    NSNumber *row = notification.object;

    
    if ([row isEqual:@0]) {

        ReadEssayViewController *EVC = [[ReadEssayViewController alloc] init];
        [self.navigationController pushViewController:EVC animated:YES];
        EssayModel *model = self.essayArr[self.item];
        EVC.idNew = model.content_id;

    }else if ([row isEqual:@1]){
        ReadSerialViewController  *SVC = [[ReadSerialViewController alloc] init];
        [self.navigationController pushViewController:SVC animated:YES];
        SerialModel *model = self.serialArr[self.item];
        SVC.idNew = model.serial_id;
        
    }else {
        ReadQuestionViewController *QVC = [[ReadQuestionViewController alloc] init];
        QuestionModel *model = self.questionArr[self.item];
        QVC.idNew = model.question_id;
        [self.navigationController pushViewController:QVC animated:YES];
    }
    
}

#pragma mark - 数据解析
- (void)dataHandle {
    
    [YHYAFNetTool GETNetWirhUrl:@"http://v3.wufazhuce.com:8000/api/reading/index" body:nil headerFile:nil response:YHYJSON Success:^(id result) {
        
        ReadDataModel *dataModel = [[ReadDataModel alloc] initWithDataSource:result[@"data"]];
        [self.dataSource addObject:dataModel];
        
        // 短篇
        for (NSDictionary *dic in dataModel.essay) {
            self.essayModel = [[EssayModel alloc] initWithDataSource:dic];
            [self.essayArr addObject:self.essayModel];
        }
           
        
        // 连载
        for (NSDictionary *dicc in dataModel.serial) {
            self.serialModel = [[SerialModel alloc] initWithDataSource:dicc];
            [self.serialArr addObject:_serialModel];
        }
        
        // 问答
        for (NSDictionary *dic in dataModel.question) {
            self.questionModel = [[QuestionModel alloc] initWithDataSource:dic];
            [self.questionArr addObject:_questionModel];
        }
        
        [self.readCollectionView reloadData];
        
    } Failure:^(NSError *error) {
        
    }];
    
    
    /**
     *  轮播图数据
     */
    [YHYAFNetTool GETNetWirhUrl:@"http://v3.wufazhuce.com:8000/api/reading/carousel" body:nil headerFile:nil response:YHYJSON Success:^(id result) {
        
        for (NSDictionary *dic in result[@"data"]) {
            self.bannerModel = [[ReadBannerModel alloc] initWithDataSource:dic];
            [self.imageArray addObject:_bannerModel.cover];
        }
        [self TopView];
        
    } Failure:^(NSError *error) {
        
    }];

}

// 颜色解码
- (UIColor *)getColor:(NSString *)hexColor {
    unsigned int red,green,blue;
    NSRange range;
    range.length = 2;
    
    range.location = 0;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];
    
    range.location = 2;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];
    
    range.location = 4;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];
    
    return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green / 255.0f) blue:(float)(blue / 255.0f) alpha:1.0f];
}

#pragma mark - 创建视图
- (void)creatView {
    
    self.view.backgroundColor = [UIColor colorWithRed:247 / 255.0 green:248 / 255.0 blue:249 / 255.0 alpha:1];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = @"阅读";
    titleLabel.font = [UIFont boldSystemFontOfSize:22];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
    
    
    
    /**
     *  创建collectionView
     */
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT * 0.7 - 117);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.readCollectionView = [[YHYBaseCollectionView alloc] initWithFrame:CGRectMake(0, HEIGHT* 0.3, WIDTH, HEIGHT * 0.7 - 117) collectionViewLayout:flowLayout];
    [self.readCollectionView registerClass:[ReadCollectionViewCell class] forCellWithReuseIdentifier:@"read"];
    self.readCollectionView.pagingEnabled = YES;
    self.readCollectionView.delegate = self;
    self.readCollectionView.dataSource = self;
    _readCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.readCollectionView];
    
    
}

#pragma mark - 头部轮播
- (void)TopView {
    
    self.topView = [[YHYBaseView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT * 0.3)];
    self.topView.backgroundColor = [UIColor cyanColor];
    
    self.picScrollView = [DCPicScrollView picScrollViewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT * 0.3) WithImageUrls:self.imageArray];
    [_topView addSubview:_picScrollView];
    [self.view addSubview:self.topView];
    [self.topView bringSubviewToFront:self.view];
    
}

#pragma mark - collectionView协议方法实现
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (_essayArr.count != 0) {
        
        return _essayArr.count;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    self.item = indexPath.row;
    
    ReadCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"read" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    
    cell.questionModel = self.questionArr[indexPath.item];
    
    cell.serialArr = self.serialArr;
        
    cell.essayModel = self.essayArr[indexPath.item];
    
    return cell;

}



//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//    
//    
//    self.item = indexPath.item;
//    
//    NSLog(@"%ld", self.item);
//    
//}

//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    
//    self.item = (self.readCollectionView.contentOffset.x / self.readCollectionView.frame.size.width);
//    NSLog(@"~~~%ld", self.item);
//}
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
